var PGRY_XQTC = ["item_qs", "item_info", "item_mark1", "item_mark2", "item_others"]; //需求提出部门
var PGRY_SJBM = ["item_tech"]; //设计部门
var PGRY_KFGS = ["item_dev", "item_test"]; //开发公司
var PGRY_YWBM = ["item_oper"]; //运维部门

var doc = getCurrentDocument();
var sqr = getItemValueAsString("sqr");
var domainid = getDomainid();
var param = getMainAndMiniSystemsInParam();

var result1 = getPeopleDistinct((getAssessPeople(domainid, param, PGRY_XQTC) + "," + sqr), ",");
var result2 = getPeopleDistinct((getAssessPeople(domainid, param, PGRY_SJBM)), ",");
var result3 = getPeopleDistinct((getAssessPeople(domainid, param, PGRY_KFGS)), ",");
var result4 = getPeopleDistinct((getAssessPeople(domainid, param, PGRY_YWBM)), ",");

doc.findItem("xqtcbm_pgry").setValue(result1);
doc.findItem("sjbm_pgry").setValue(result2);
doc.findItem("kfgs_pgry").setValue(result3);
doc.findItem("ywbm_pgry").setValue(result4);

setFzr();

/**
 * 获取评估人员
 * @param domainid 
 * @param param 
 * @param array 
 */
function getAssessPeople(domainid, param, array) {
    var resultStr = "";
    if (null != array && array.length > 0) {
        var sql = " select replace( ";
        if (array.length == 1) {
            sql = sql + "group_concat(" + array[0];
        } else {
            sql = sql + "concat ( group_concat(" + array[0] + ")";
            for (var i = 1; i < array.length; i++) {
                sql = sql + " , ';', group_concat(" + array[i] + ")";
            }
        }
        sql = sql + "), ';', ',') as item_data, '" + domainid + "' as domainid from tlk_system_manager where " +
            "(case when (item_system_son is null or item_system_son = '')" +
            "then (item_system) else concat(item_system, '_', item_system_son) end ) " +
            "in ( '" + param + "' )";
        var result = findBySQL(sql);
        if (null != result) {
            resultStr = result.getItemValueAsString("data");
        }
    }
    return resultStr;
}

function getPeopleDistinct(str, conn) {
    var result = "";
    var set = createObject("java.util.HashSet");
    var array = splitText(str, conn);
    for (var i = 0; i < array.length; i++) {
        var user = array[i];
        if (isNotNull(user)) {
            set.add(user);
        }
    }
    if (set.size() > 0) {
        var iter = set.iterator();
        while (iter.hasNext()) {
            if (isNotNull(result)) {
                result = result + ";" + iter.next();
            } else {
                result = iter.next();
            }

        }
    }
    return result;
}


/**
 * @returns 主配系统通过 “','” 符号连接的字符串
 */
function getMainAndMiniSystemsInParam() {
    var nameStr = "";
    var mainSys = getItemValueAsString("ztxt_mc"); //1. 获取主系统名称
    nameStr = nameStr + mainSys;
    var viceSysNameArray = splitText(getItemValueAsString("phxt_mc"), ";"); //2. 获取配合系统名称
    if ((null != viceSysNameArray) && (viceSysNameArray.length > 0)) { //3. 整合
        for (var i = 0; i < viceSysNameArray.length; i++) {
            nameStr += "','" + viceSysNameArray[i];
        }
    }
    var param = nameStr;
    return param;
}


function setFzr() {
    var xqdfzr = "";
    var type = getItemValueAsString("qzxm");
    if (isNotNull(type)) {
        if ("是".equals(type)) {
            xqdfzr = getItemValueAsString("sqr");
        } else {
            var domainid = getDomainid();
            var main_sys = "";
            main_sys = getItemValueAsString("ztxt_mc");
            sql = "select " +
                "group_concat(item_tech) as item_data " +
                ", '" + domainid + "' as domainid " +
                "from tlk_system_manager  " +
                "where( " +
                "case when item_system_son is null or item_system_son = '' " +
                "then " +
                "item_system " +
                "else " +
                "concat(item_system, '_', item_system_son) " +
                "end " +
                ") in ( '" + main_sys + "' )";
            xqdfzr = findBySQL(sql).getItemValueAsString("data");
        }
    }
    getCurrentDocument().findItem("dzxqdfzr").setValue(xqdfzr);
}